/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: voltorb flip!
@author: ozzy27
@description: None
@tags: []
@addedOn: 2025-00-00
*/

const melody = tune`
379.746835443038: C5/379.746835443038 + E4-379.746835443038 + C4~379.746835443038,
379.746835443038: G4/379.746835443038 + D4-379.746835443038,
379.746835443038: C5/379.746835443038 + E4-379.746835443038 + C4~379.746835443038,
379.746835443038: G4/379.746835443038 + D4-379.746835443038,
379.746835443038: D5/379.746835443038 + C4-379.746835443038 + D4~379.746835443038,
379.746835443038: B4/379.746835443038 + D4-379.746835443038,
379.746835443038: D5/379.746835443038 + C4-379.746835443038 + D4~379.746835443038,
379.746835443038: B4/379.746835443038 + D4-379.746835443038,
379.746835443038: E5/379.746835443038 + G4/379.746835443038 + D4-379.746835443038 + C4~379.746835443038,
379.746835443038: D5/379.746835443038 + F4/379.746835443038 + C4-379.746835443038,
379.746835443038: E5/379.746835443038 + A4/379.746835443038 + D4-379.746835443038 + C4~379.746835443038,
379.746835443038: C5/379.746835443038 + G4/379.746835443038 + D4-379.746835443038,
379.746835443038: C5/379.746835443038 + E5/379.746835443038 + C4-379.746835443038 + D4~379.746835443038,
379.746835443038: D5/379.746835443038 + B4/379.746835443038 + D4-379.746835443038,
379.746835443038: C5/379.746835443038 + E4-379.746835443038 + A4-379.746835443038 + C4~379.746835443038,
379.746835443038: C5-379.746835443038 + E5-379.746835443038,
379.746835443038: F5^379.746835443038 + C5^379.746835443038 + A4~379.746835443038,
379.746835443038: G4/379.746835443038 + E5-379.746835443038,
379.746835443038: D4/379.746835443038 + D5-379.746835443038 + B4~379.746835443038,
379.746835443038: G4/379.746835443038 + E5-379.746835443038,
379.746835443038: D4/379.746835443038 + D5-379.746835443038 + B4~379.746835443038,
379.746835443038: A4/379.746835443038 + C5-379.746835443038,
379.746835443038: F4/379.746835443038 + D5-379.746835443038 + A4~379.746835443038,
379.746835443038: A4/379.746835443038 + C5-379.746835443038,
379.746835443038: F4/379.746835443038 + D5-379.746835443038 + A4~379.746835443038,
379.746835443038: B4/379.746835443038 + D4/379.746835443038 + D5-379.746835443038,
379.746835443038: C4/379.746835443038 + A4/379.746835443038 + C5-379.746835443038 + B4~379.746835443038,
379.746835443038: B4/379.746835443038 + E4/379.746835443038 + D5-379.746835443038,
379.746835443038: G4/379.746835443038 + D5-379.746835443038 + A4~379.746835443038,
379.746835443038: G4/379.746835443038 + C5-379.746835443038,
379.746835443038: A4/379.746835443038 + F4/379.746835443038 + D5-379.746835443038 + B4~379.746835443038,
379.746835443038: G4/379.746835443038 + E5-379.746835443038`;
const playback=playTune(melody,Infinity);
function makeLegendSprite(topnum, bottomnum) {
  let top;
  let bottom;

  if (topnum == 0) {
  top = bitmap`
................
.....55555......
.....55.55......
.....55.55......
.....55.55......
.....55.55......
.....55555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 1) {
  top = bitmap`
................
.......55.......
......555.......
.......55.......
.......55.......
.......55.......
......5555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 2) {
  top = bitmap`
................
......555.......
.....55555......
........55......
.......55.......
......55........
.....55555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 3) {
  top = bitmap`
................
......555.......
.....55555......
........55......
.......55.......
........55......
.....55555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 4) {
  top = bitmap`
................
........55......
.....55.55......
.....55.55......
.....55555......
........55......
........55......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 5) {
  top = bitmap`
................
......555.......
.....55555......
.....55.........
.....5555.......
........55......
.....55555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 6) {
  top = bitmap`
................
......555.......
.....55555......
.....55.........
.....55555......
.....55..5......
......5555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 7) {
  top = bitmap`
................
.....5555.......
.....55555......
........55......
......555.......
.......55.......
.......55.......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 8) {
  top = bitmap`
................
......555.......
.....55555......
.....55.55......
......555.......
.....55.55......
.....55555......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 9) {
  top = bitmap`
................
......5555......
.....55.55......
.....55.55......
......5555......
........55......
........55......
................
................
................
................
................
................
................
................
................`
} else if (topnum == 10) {
  top = bitmap`
................
....55..55555...
...555..55.55...
....55..55.55...
....55..55.55...
....55..55.55...
...5555.55555...
................
................
................
................
................
................
................
................
................`
} else if (topnum == 11) {
  top = bitmap`
................
....55...55.....
...555..555.....
....55...55.....
....55...55.....
....55...55.....
...5555.5555....
................
................
................
................
................
................
................
................
................`
} else if (topnum == 12) {
  top = bitmap`
................
....55...555....
...555..55555...
....55.....55...
....55....55....
....55...55.....
...5555.55555...
................
................
................
................
................
................
................
................
................`
} else if (topnum == 13) {
  top = bitmap`
................
....55...555....
...555..55555...
....55.....55...
....55....55....
....55.....55...
...5555.55555...
................
................
................
................
................
................
................
................
................`
} else if (topnum == 14) {
  top = bitmap`
................
....55.....55...
...555..55.55...
....55..55.55...
....55..55555...
....55.....55...
...5555....55...
................
................
................
................
................
................
................
................
................`
} else if (topnum == 15) {
  top = bitmap`
................
....55...555....
...555..55555...
....55..55......
....55..5555....
....55.....55...
...5555.55555...
................
................
................
................
................
................
................
................
................`
}


  if (bottomnum == 0) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
.....55555......
.....55.55......
.....55.55......
.....55.55......
.....55.55......
.....55555......
................
0000000000000000`
} else if (bottomnum == 1) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
.......55.......
......555.......
.......55.......
.......55.......
.......55.......
......5555......
................
0000000000000000`
} else if (bottomnum == 2) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
......555.......
.....55555......
........55......
.......55.......
......55........
.....55555......
................
0000000000000000`
} else if (bottomnum == 3) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
......555.......
.....55555......
........55......
.......55.......
........55......
.....55555......
................
0000000000000000`
} else if (bottomnum == 4) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
........55......
.....55.55......
.....55.55......
.....55555......
........55......
........55......
................
0000000000000000`
} else if (bottomnum == 5) {
  bottom = bitmap`
................
................
................
................
................
................
................
................
......555.......
.....55555......
.....55.........
.....5555.......
........55......
.....55555......
................
0000000000000000`
}


  const full = [];

  for (let y = 0; y < 8; y++) {
    for (let x = 0; x < 16; x++) {
      full.push(top[x + y * 16]);
    }
  }

  for (let y = 8; y < 16; y++) {
    for (let x = 0; x < 16; x++) {
      full.push(bottom[x + y * 16]);
    }
  }
  for (let i = 0; i<16; i++) {
     full.push(7);
  }

  return full;
}

const player = "p";
const first = "a";
const second = "b";
const third = "c";
const fourth = "d";
const fifth = "e";
const sixth = "f";
const seventh = "g";
const eighth = "h";
const ninth = "i";
const tenth = "j";
const blank = "n";
const one = "1";
const two ="2";
const three = "3";
const voltorb="0";
const win="w";
const lose="l";
const ozzy="z";

setLegend(
  [player, bitmap`
.33333333333333.
33CCCC333333333C
3C............3C
3C............3C
3C............3C
3C............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
C3333333333333CC
.CCCCCCCCCCCCCC.`],
  [first, bitmap`${makeLegendSprite(12,2).join("")}`],
  [second, bitmap`${makeLegendSprite(12,2).join("")}`],
  [third, bitmap`${makeLegendSprite(12,2).join("")}`],
  [fourth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [fifth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [sixth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [seventh, bitmap`${makeLegendSprite(12,2).join("")}`],
  [eighth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [ninth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [tenth, bitmap`${makeLegendSprite(12,2).join("")}`],
  [blank, bitmap`
44DD4444DDDD44DD
4DDD4444DDDD444D
DDDD4444DDDD4444
DDDD4444DDDD4444
4444DDDD4444DDDD
4444DDDD4444DDDD
4444DDDD4444DDDD
4444DDDD4444DDDD
DDDD4444DDDD4444
DDDD4444DDDD4444
DDDD4444DDDD4444
DDDD4444DDDD4444
4444DDDD4444DDDD
4444DDDD4444DDDD
D444DDDD4444DDD4
DD44DDDD4444DD44`],
  [one, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966663336666666
6666633336666699
6666333336666699
6666663336666699
6666663336666699
9966663336666666
9966663336666666
9966333333366666
9966333333366666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [two, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966633333666666
6666333333366699
6666336633366699
6666666633366699
6666666333666699
9966663336666666
9966633366666666
9966333333366666
9966333333366666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [three, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966633333666666
6666333333366699
6666336633366699
6666666633366699
6666663333666699
9966666633666666
9966336633366666
9966333333366666
9966633333666666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [voltorb, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966600000066666
6666033333306699
6660033333300699
6660303333030699
6660320330230699
9960120330210666
9960111111110666
9960111111110666
9966011111106666
6666600000066699
6666666666666699
9666999966669996
9966999966669966`],
  [ozzy,bitmap`
................
................
....CCCCCCCC....
....CCCCCCCC....
....66006600....
....66006600....
....66006600....
....66006600....
....66666666....
....66666666....
......3333......
......3333......
......5555......
......5555......
................
................`],
  [win, bitmap`
................
................
................
............DDD.
...........D22D.
..........D244D.
.........D244D..
........D444D...
.DDD...D444D....
.D22D.D444D.....
.D244D444D......
..D44444D.......
...D444D........
....DDD.........
................
................`],
  [lose,bitmap`
................
.CCC........CCC.
.C22C......C22C.
.C233C....C233C.
..C333C..C333C..
...C333CC333C...
....C333333C....
.....C3333C.....
.....C3333C.....
....C333333C....
...C333CC333C...
..C233C..C333C..
.C233C....C333C.
.C23C......C33C.
.CCC........CCC.
................`]
);

setSolids([]);

let level = 0;
const levels = [
  map`
nnnnna
nnnnnb
nnnnnc
nnnnnd
nnnnne
jihgfp`
];


  const numbers=[];
  const opened=[];

function newNums(numbers) {
for (let i=0;i<36;i++) {
  if (Math.random()<0.5) {
    numbers.push(1);
  } else if (Math.random()<0.5) {
    numbers.push(0);
  } else if (Math.random()<0.5) {
    numbers.push(3);
  } else {
    numbers.push(2);
  }
}
  setMap(levels[level]);
  //aaaaa
  for (let i=0; i<10; i++) {
    
    ["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"][i]
  }
  
}
for (let i=0;i<36;i++) {opened.push(false);}
newNums(numbers);
function updateSideNums() {
  setLegend(
  [player, bitmap`
.33333333333333.
33CCCC333333333C
3C............3C
3C............3C
3C............3C
3C............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
33............3C
C3333333333333CC
.CCCCCCCCCCCCCC.`],
  [first, bitmap`${makeLegendSprite(numbers[0]+numbers[1]+numbers[2]+numbers[3]+numbers[4], (numbers[0] == 0) + (numbers[1] == 0) + (numbers[2] == 0) + (numbers[3] == 0) + (numbers[4] == 0)).join("")}`],
    [second, bitmap`${makeLegendSprite(numbers[6]+numbers[7]+numbers[8]+numbers[9]+numbers[10], (numbers[6] == 0) + (numbers[7] == 0) + (numbers[8] == 0) + (numbers[9] == 0) + (numbers[10] == 0)).join("")}`],
    [third, bitmap`${makeLegendSprite(numbers[12]+numbers[13]+numbers[14]+numbers[15]+numbers[16], (numbers[12] == 0) + (numbers[13] == 0) + (numbers[14] == 0) + (numbers[15] == 0) + (numbers[16] == 0)).join("")}`],
    [fourth, bitmap`${makeLegendSprite(numbers[18]+numbers[19]+numbers[20]+numbers[21]+numbers[22], 
    (numbers[18] == 0) + (numbers[19] == 0) + (numbers[20] == 0) + (numbers[21] == 0) + (numbers[22] == 0)).join("")}`],

[fifth, bitmap`${makeLegendSprite(numbers[24]+numbers[25]+numbers[26]+numbers[27]+numbers[28], 
    (numbers[24] == 0) + (numbers[25] == 0) + (numbers[26] == 0) + (numbers[27] == 0) + (numbers[28] == 0)).join("")}`],

[sixth, bitmap`${makeLegendSprite(numbers[4]+numbers[10]+numbers[16]+numbers[22]+numbers[28], 
    (numbers[4] == 0) + (numbers[10] == 0) + (numbers[16] == 0) + (numbers[22] == 0) + (numbers[28] == 0)).join("")}`],

[seventh, bitmap`${makeLegendSprite(numbers[3]+numbers[9]+numbers[15]+numbers[21]+numbers[27], 
    (numbers[3] == 0) + (numbers[9] == 0) + (numbers[15] == 0) + (numbers[21] == 0) + (numbers[27] == 0)).join("")}`],

[tenth, bitmap`${makeLegendSprite(numbers[0]+numbers[6]+numbers[12]+numbers[18]+numbers[24], 
    (numbers[0] == 0) + (numbers[6] == 0) + (numbers[12] == 0) + (numbers[18] == 0) + (numbers[24] == 0)).join("")}`],

[ninth, bitmap`${makeLegendSprite(numbers[1]+numbers[7]+numbers[13]+numbers[19]+numbers[25], 
    (numbers[1] == 0) + (numbers[7] == 0) + (numbers[13] == 0) + (numbers[19] == 0) + (numbers[25] == 0)).join("")}`],

[eighth, bitmap`${makeLegendSprite(numbers[2]+numbers[8]+numbers[14]+numbers[20]+numbers[26], 
    (numbers[2] == 0) + (numbers[8] == 0) + (numbers[14] == 0) + (numbers[20] == 0) + (numbers[26] == 0)).join("")}`],

    [blank, bitmap`
44DD4444DDDD44DD
4DDD4444DDDD444D
DDDD4444DDDD4444
DDDD4444DDDD4444
4444DDDD4444DDDD
4444DDDD4444DDDD
4444DDDD4444DDDD
4444DDDD4444DDDD
DDDD4444DDDD4444
DDDD4444DDDD4444
DDDD4444DDDD4444
DDDD4444DDDD4444
4444DDDD4444DDDD
4444DDDD4444DDDD
D444DDDD4444DDD4
DD44DDDD4444DD44`],
  [one, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966663336666666
6666633336666699
6666333336666699
6666663336666699
6666663336666699
9966663336666666
9966663336666666
9966333333366666
9966333333366666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [two, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966633333666666
6666333333366699
6666336633366699
6666666633366699
6666666333666699
9966663336666666
9966633366666666
9966333333366666
9966333333366666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [three, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966633333666666
6666333333366699
6666336633366699
6666666633366699
6666663333666699
9966666633666666
9966336633366666
9966333333366666
9966633333666666
6666666666666699
6666666666666699
9666999966669996
9966999966669966`],
  [voltorb, bitmap`
6699666699996699
6999666699996669
9966666666666666
9966600000066666
6666033333306699
6660033333300699
6660303333030699
6660320330230699
9960120330210666
9960111111110666
9960111111110666
9966011111106666
6666600000066699
6666666666666699
9666999966669996
9966999966669966`],
    [ozzy,bitmap`
................
................
....CCCCCCCC....
....CCCCCCCC....
....66006600....
....66006600....
....66006600....
....66006600....
....66666666....
....66666666....
......3333......
......3333......
......5555......
......5555......
................
................`],
  [win, bitmap`
................
................
................
............DDD.
...........D22D.
..........D244D.
.........D244D..
........D444D...
.DDD...D444D....
.D22D.D444D.....
.D244D444D......
..D44444D.......
...D444D........
....DDD.........
................
................`],
  [lose,bitmap`
................
.CCC........CCC.
.C22C......C22C.
.C233C....C233C.
..C333C..C333C..
...C333CC333C...
....C333333C....
.....C3333C.....
.....C3333C.....
....C333333C....
...C333CC333C...
..C233C..C333C..
.C233C....C333C.
.C23C......C33C.
.CCC........CCC.
................`]
);
}

function check() {
  for (let i=0;i<36;i++){
      if (numbers[i]>1 & !opened[i] & (Math.round((i+1)/6)*6!=i+1 & i<30)) {
        return false;
      }
    }
  return true;
}

setPushables({
  [player]: []
});
getFirst(player).x=2;
getFirst(player).y=2;

onInput("s", () => {
  if (getFirst(player).y<4) {
  getFirst(player).y += 1;}
});
onInput("w", () => {
  getFirst(player).y -= 1;
});
let easportsitsinthegame=true;
onInput("a", () => {
  getFirst(player).x -= 1;
});
onInput("d", () => {
  if (getFirst(player).x<4) {
  getFirst(player).x += 1;}
});
onInput("i", () => { if (!easportsitsinthegame) {
  numbers.length=0;
  opened.length=0;
  saveX=getFirst(player).x;
  saveY=getFirst(player).y;
  newNums(numbers);
  for (let i=0;i<36;i++) {opened.push(false);}
  getFirst(player).x=saveX;
  getFirst(player).y=saveY;
addSprite(5,5,"z");
  updateSideNums();
}});
numbers.length=0;
  opened.length=0;
  saveX=getFirst(player).x;
  saveY=getFirst(player).y;
  newNums(numbers);
  for (let i=0;i<36;i++) {opened.push(false);}
  getFirst(player).x=saveX;
  getFirst(player).y=saveY;
addSprite(5,5,"z");
  updateSideNums();
let newlevel=false;
onInput("j", () => { if (easportsitsinthegame) {
  posnum=(getFirst(player).y*6)+getFirst(player).x;
  if (!opened[posnum]) {
    opened[posnum]=true;
    savedX=getFirst(player).x;
    savedY=getFirst(player).y;
    
    let newmap = [];
    for (let y = 0;y<5;y++) {
      for (let x = 0;x<6;x++) {
        if (x==5) {
          newmap.push("abcde"[y]);
        }
        else if (opened[(y*6)+x]) {
            newmap.push(numbers[(y*6)+x].toString());
        } else {
          newmap.push("n");
        }
      }
      newmap.push("\n");
    }
      if (check()){
        opened.length=0;
        saveX=getFirst(player).x;
        saveY=getFirst(player).y;
        newlevel=true;
        newmap.length=0;
        for (let y = 0;y<5;y++) {
          for (let x = 0;x<6;x++) {
            if (x==5) {
              newmap.push("abcde"[y]);
            }
            else{
                newmap.push(numbers[(y*6)+x].toString());
            } 
          }
          newmap.push("\n");
        }
        newmap.push("jihgfp");
        getFirst(player).x=savedX;
    getFirst(player).y=savedY;
        setMap(map`${newmap.join("")}`);
        addSprite(5,5,"w");
        easportsitsinthegame=false;
      } else {
        if (numbers[(getFirst(player).y*6)+getFirst(player).x]==0){
        opened.length=0;
        saveX=getFirst(player).x;
        saveY=getFirst(player).y;
        newlevel=true;
        newmap.length=0;
        for (let y = 0;y<5;y++) {
          for (let x = 0;x<6;x++) {
            if (x==5) {
              newmap.push("abcde"[y]);
            }
            else{
                newmap.push(numbers[(y*6)+x].toString());
            } 
          }
          newmap.push("\n");
        }
        newmap.push("jihgfp");
        getFirst(player).x=savedX;
    getFirst(player).y=savedY;
        setMap(map`${newmap.join("")}`);
        addSprite(5,5,"l");
        easportsitsinthegame=false;
      } else {addSprite(5,5,"z");}
      }
    if (!newlevel){
    newmap.push("jihgfp");
    setMap(map`${newmap.join("")}`);
    getFirst(player).x=savedX;
    getFirst(player).y=savedY;
      addSprite(5,5,"z");
    }
    else {
    newlevel=false;
      getFirst(player).x=savedX;
      getFirst(player).y=savedY;
    }
  }
} else {
  numbers.length=0;
  opened.length=0;
  saveX=getFirst(player).x;
  saveY=getFirst(player).y;
  newNums(numbers);
  for (let i=0;i<36;i++) {opened.push(false);}
  getFirst(player).x=saveX;
  getFirst(player).y=saveY;
addSprite(5,5,"z");
  updateSideNums();
  easportsitsinthegame=true;
}
});
onInput("k", () => { if (!easportsitsinthegame) {
  numbers.length=0;
  opened.length=0;
  saveX=getFirst(player).x;
  saveY=getFirst(player).y;
  newNums(numbers);
  for (let i=0;i<36;i++) {opened.push(false);}
  getFirst(player).x=saveX;
  getFirst(player).y=saveY;
addSprite(5,5,"z");
  updateSideNums();
}});

onInput("l", () => { if (!easportsitsinthegame) {
  numbers.length=0;
  opened.length=0;
  saveX=getFirst(player).x;
  saveY=getFirst(player).y;
  newNums(numbers);
  for (let i=0;i<36;i++) {opened.push(false);}
  getFirst(player).x=saveX;
  getFirst(player).y=saveY;
addSprite(5,5,"z");
  updateSideNums();
}});
afterInput(() => {
});
